package com.swak.paxos.store;

import java.util.List;

import com.swak.mmap.MappedFileAllocator;
import com.swak.paxos.PaxosConfig;
import com.swak.reactivex.threads.EventLoop;
import com.swak.reactivex.threads.SwakThreadFactory;
import com.swak.utils.Lists;

/**
 * PaxosStorageManager.
 * 
 * 对存储空间进行管理，管理所有的分组的存储空间.
 * 
 * 刷盘, 合并, 检查点, 清除
 * 
 * @author 618lf
 */
public class PaxosStorageManager {

	private static final SwakThreadFactory Factory = new SwakThreadFactory("Paxos.Storage-ALL", false);

	private final PaxosConfig config;
	private final List<PaxosStorage> storages;
	private final EventLoop eventLoop;
	private final MappedFileAllocator mappedFileAllocator;

	public PaxosStorageManager(PaxosConfig config) {
		this.config = config;
		this.storages = Lists.newArrayList();
		this.eventLoop = EventLoop.newTryBestEventLoop(Factory);
		this.mappedFileAllocator = new MappedFileAllocator(this.eventLoop);
	}

	public PaxosStorage newPaxosStorage(int groupID) {
		PaxosStorage paxosStorage = new PaxosStorage(groupID, this.config, this.mappedFileAllocator);
		this.storages.add(paxosStorage);
		return paxosStorage;
	}
}